﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace SalesSys.Comm.Utility
{

    public static  class EnumHelp
    {
        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="valueEnum"></param>
        /// <returns></returns>
        public static string GetDescInfo(this Enum valueEnum)
        {
            Type enumType = valueEnum.GetType();
            var name = Enum.GetName(enumType, valueEnum);
            var info = enumType.GetField(name);
            return GetDescInfo(info);
        }

        private static string GetDescInfo(FieldInfo info)
        {
            var attr = Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute)) as DescriptionAttribute;

            return attr == null ? null : attr.Description;
        }

        #region
        /// <summary>
        /// 把值转换成枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ConvertToEnum<T>(object value)
        {
            T t = default(T);
            t = (T)Enum.Parse(typeof(T), value.ToString());
            return t;
        }


        #endregion
    }
}
